package com.itheima.reggie.service.impl;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.*;
import com.itheima.reggie.domain.User;
import com.itheima.reggie.mapper.UserMapper;
import com.itheima.reggie.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;

import java.time.Duration;
import java.util.HashMap;
import java.util.Map;

//用户
@Service        //将service层交给ioc容器
@Transactional  //事务管理器
public class UserServiceImpl implements UserService {

    //依赖注入阿里云sms
    @Autowired
    private SmsTemplate smsTemplate;

    //依赖注入redis父工具类
    @Autowired
    private RedisTemplate redisTemplate;

    //依赖注入mapper层
    @Autowired
    private UserMapper userMapper;

    //发送验证码
    @Override
    public void sendMsg(String phone) {
        //业务处理
        //1.生成6位随机数
        String code = RandomUtil.randomNumbers(6);  //阿里巴巴随机数生成
        code = "123";       //TODO 开发期间写死
        //2. 调用阿里云sms模板发送验证码
        //smsTemplate.sendSms(phone,code);     //TODO 开发期间注释
        //3. 将验证码存入redis (5分钟)
        redisTemplate.opsForValue().set(Constant.CODE_HEADER + phone, code, Duration.ofSeconds(300));
    }

    //登录/注册验证
    @Override
    public ResultInfo login(String phone, String code) {
        //业务处理
        //1. 判断验证码是否正确
        //1-1 取出redis
        String codeFromRedis = (String) redisTemplate.opsForValue().get(Constant.CODE_HEADER + phone);
        //1-2 比较
        if (!StrUtil.equals(codeFromRedis, code)) {
            // throw new CustomException("验证码不正确");
            return ResultInfo.error("验证码不正确");
        }
        //2. 根据手机查询数据库
        User user = userMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getPhone, phone));

        if (user == null) { //3. 新用户
            //注册
            user = new User();
            user.setPhone(phone);
            user.setStatus(1);
            userMapper.insert(user);    //存入表中
        } else {     //4. 老用户
            //校验状态
            if (user.getStatus() == 0) {
                return ResultInfo.error("此用户已被冻结,请联系管理员");
            }
        }

        //5. jwt制作token
        Map<String, Object> claims = new HashMap<>();
        claims.put("id", user.getId());
        claims.put("phone", user.getPhone());
        String token = JwtUtil.createToken(claims);
        //6. 将user存redis        ==>  判断登录状态 + 续期
        redisTemplate.opsForValue().set(Constant.USER_PHONE_CODE + user.getPhone(), user, Duration.ofDays(1));
        //7. 清除redis的验证码
        redisTemplate.delete(Constant.CODE_HEADER + phone);
        //8. 返回token
        return ResultInfo.success(token);
    }


    //注销登录
    @Override
    public void logout() {
        //清除redis中的user信息
        redisTemplate.delete(Constant.USER_PHONE_CODE + UserHolder.get().getPhone());
    }


}
